
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>[实现记事本] · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-tomorrow.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="part3.html" />
    
    
    <link rel="prev" href="part1.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter0/introduction.html">
            
                <a href="../chapter0/introduction.html">
            
                    
                    第零章：实验环境说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter1/introduction.html">
            
                <a href="../chapter1/introduction.html">
            
                    
                    第一章：独立式可执行程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter1/part1.html">
            
                <a href="../chapter1/part1.html">
            
                    
                    [安装 nightly rust]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter1/part2.html">
            
                <a href="../chapter1/part2.html">
            
                    
                    使用包管理器 cargo 创建 rust binary 项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter1/part3.html">
            
                <a href="../chapter1/part3.html">
            
                    
                    移除标准库依赖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter1/part4.html">
            
                <a href="../chapter1/part4.html">
            
                    
                    [移除 runtime 依赖]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter1/part5.html">
            
                <a href="../chapter1/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter2/introduction.html">
            
                <a href="../chapter2/introduction.html">
            
                    
                    第二章：最小化内核
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter2/part1.html">
            
                <a href="../chapter2/part1.html">
            
                    
                    使用目标三元组描述目标平台
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter2/part2.html">
            
                <a href="../chapter2/part2.html">
            
                    
                    编译、生成内核镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter2/part3.html">
            
                <a href="../chapter2/part3.html">
            
                    
                    使用链接脚本指定内存布局
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter2/part4.html">
            
                <a href="../chapter2/part4.html">
            
                    
                    [重写程序入口点 -start]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter2/part5.html">
            
                <a href="../chapter2/part5.html">
            
                    
                    [使用 Qemu 运行内核]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter2/part6.html">
            
                <a href="../chapter2/part6.html">
            
                    
                    封装 SBI 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter2/part7.html">
            
                <a href="../chapter2/part7.html">
            
                    
                    [实现格式化输出]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter2/part8.html">
            
                <a href="../chapter2/part8.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter3/introduction.html">
            
                <a href="../chapter3/introduction.html">
            
                    
                    第三章：中断
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter3/part1.html">
            
                <a href="../chapter3/part1.html">
            
                    
                    rv64 中断介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter3/part2.html">
            
                <a href="../chapter3/part2.html">
            
                    
                    [手动触发断点中断]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter3/part3.html">
            
                <a href="../chapter3/part3.html">
            
                    
                    程序运行上下文环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../chapter3/part4.html">
            
                <a href="../chapter3/part4.html">
            
                    
                    [实现上下文环境保存与恢复]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../chapter3/part5.html">
            
                <a href="../chapter3/part5.html">
            
                    
                    [时钟中断]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../chapter3/part6.html">
            
                <a href="../chapter3/part6.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter4/introduction.html">
            
                <a href="../chapter4/introduction.html">
            
                    
                    第四章：内存管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter4/part1.html">
            
                <a href="../chapter4/part1.html">
            
                    
                    [物理内存探测与管理]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter4/part2.html">
            
                <a href="../chapter4/part2.html">
            
                    
                    [动态内存分配]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter4/part3.html">
            
                <a href="../chapter4/part3.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter5/introduction.html">
            
                <a href="../chapter5/introduction.html">
            
                    
                    第五章：内存虚拟化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/part1.html">
            
                <a href="../chapter5/part1.html">
            
                    
                    页表：从虚拟内存到物理内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter5/part2.html">
            
                <a href="../chapter5/part2.html">
            
                    
                    [内核初始映射]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter5/part3.html">
            
                <a href="../chapter5/part3.html">
            
                    
                    内核重映射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter5/part4.html">
            
                <a href="../chapter5/part4.html">
            
                    
                    内核重映射实现之一：页表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter5/part5.html">
            
                <a href="../chapter5/part5.html">
            
                    
                    内核重映射实现之二：MemorySet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../chapter5/part6.html">
            
                <a href="../chapter5/part6.html">
            
                    
                    [内核重映射实现之三：完结]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../chapter5/part7.html">
            
                <a href="../chapter5/part7.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter6/introduction.html">
            
                <a href="../chapter6/introduction.html">
            
                    
                    第六章：内核线程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter6/part1.html">
            
                <a href="../chapter6/part1.html">
            
                    
                    线程状态与保存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter6/part2.html">
            
                <a href="../chapter6/part2.html">
            
                    
                    线程切换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter6/part3.html">
            
                <a href="../chapter6/part3.html">
            
                    
                    内核线程初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter6/part4.html">
            
                <a href="../chapter6/part4.html">
            
                    
                    [内核线程创建与切换测试]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter6/part5.html">
            
                <a href="../chapter6/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter7/introduction.html">
            
                <a href="../chapter7/introduction.html">
            
                    
                    第七章：线程调度
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter7/part1.html">
            
                <a href="../chapter7/part1.html">
            
                    
                    线程池与线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter7/part2.html">
            
                <a href="../chapter7/part2.html">
            
                    
                    内核调度线程 idle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter7/part3.html">
            
                <a href="../chapter7/part3.html">
            
                    
                    线程调度之 Round Robin 算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter7/part4.html">
            
                <a href="../chapter7/part4.html">
            
                    
                    [线程调度测试]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter7/part5.html">
            
                <a href="../chapter7/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter8/introduction.html">
            
                <a href="../chapter8/introduction.html">
            
                    
                    第八章：进程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter8/part1.html">
            
                <a href="../chapter8/part1.html">
            
                    
                    [编写用户程序]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter8/part1_1.html">
            
                <a href="../chapter8/part1_1.html">
            
                    
                    合并内核与应用程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter8/part2.html">
            
                <a href="../chapter8/part2.html">
            
                    
                    在内核中实现系统调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter8/part3.html">
            
                <a href="../chapter8/part3.html">
            
                    
                    创建虚拟内存空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../chapter8/part4.html">
            
                <a href="../chapter8/part4.html">
            
                    
                    [创建进程]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="../chapter8/part5.html">
            
                <a href="../chapter8/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="introduction.html">
            
                <a href="introduction.html">
            
                    
                    第九章：文件系统
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="part1.html">
            
                <a href="part1.html">
            
                    
                    [使用文件系统]
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.11.2" data-path="part2.html">
            
                <a href="part2.html">
            
                    
                    [实现记事本]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="part3.html">
            
                <a href="part3.html">
            
                    
                    [实现终端]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="part4.html">
            
                <a href="part4.html">
            
                    
                    [文件读写]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="part5.html">
            
                <a href="part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter10/introduction.html">
            
                <a href="../chapter10/introduction.html">
            
                    
                    第十章：同步互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../exercise/introduction.html">
            
                <a href="../exercise/introduction.html">
            
                    
                    练习题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../exercise/part1.html">
            
                <a href="../exercise/part1.html">
            
                    
                    1. 中断异常
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="../exercise/part2.html">
            
                <a href="../exercise/part2.html">
            
                    
                    2. 物理内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="../exercise/part3.html">
            
                <a href="../exercise/part3.html">
            
                    
                    3. 虚拟内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="../exercise/part4.html">
            
                <a href="../exercise/part4.html">
            
                    
                    4. 线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.5" data-path="../exercise/part5.html">
            
                <a href="../exercise/part5.html">
            
                    
                    5. 用户进程（+ 虚拟内存管理 + 线程管理）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.6" data-path="../exercise/part6.html">
            
                <a href="../exercise/part6.html">
            
                    
                    6. CPU 调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.7" data-path="../exercise/part7.html">
            
                <a href="../exercise/part7.html">
            
                    
                    7. 同步互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.8" data-path="../exercise/part8.html">
            
                <a href="../exercise/part8.html">
            
                    
                    8. 文件系统
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../appendix/introduction.html">
            
                <a href="../appendix/introduction.html">
            
                    
                    附录
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../appendix/inline_asm.html">
            
                <a href="../appendix/inline_asm.html">
            
                    
                    内联汇编
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../appendix/install_rust.html">
            
                <a href="../appendix/install_rust.html">
            
                    
                    安装 rust
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >[实现记事本]</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x5B9E;&#x73B0;&#x8BB0;&#x4E8B;&#x672C;">&#x5B9E;&#x73B0;&#x8BB0;&#x4E8B;&#x672C;</h2>
<ul>
<li><a href="https://github.com/rcore-os/rCore_tutorial/tree/ch9-pa2" target="_blank">&#x4EE3;&#x7801;</a></li>
</ul>
<p>&#x4E3A;&#x4E86;&#x5B9E;&#x73B0;&#x4E0A;&#x8282;&#x4E2D;&#x4EA4;&#x4E92;&#x5F0F;&#x7EC8;&#x7AEF;&#x7684;&#x76EE;&#x6807;&#xFF0C;&#x5148;&#x4E0D;&#x7BA1;&#x8FD0;&#x884C;&#x7A0B;&#x5E8F;&#xFF0C;&#x6211;&#x4EEC;&#x9996;&#x5148;&#x8981;&#x80FD;&#x591F;&#x901A;&#x8FC7;&#x952E;&#x76D8;&#x5411;&#x7EC8;&#x7AEF;&#x7A0B;&#x5E8F;&#x4E2D;&#x8F93;&#x5165;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#xFF0C;&#x5B83;&#x80FD;&#x591F;&#x63A5;&#x53D7;&#x952E;&#x76D8;&#x7684;&#x8F93;&#x5165;&#xFF0C;&#x5E76;&#x5C06;&#x952E;&#x76D8;&#x8F93;&#x5165;&#x7684;&#x5B57;&#x7B26;&#x663E;&#x793A;&#x5728;&#x5C4F;&#x5E55;&#x4E0A;&#x3002;&#x8FD9;&#x4E0D;&#x80FD;&#x53EB;&#x4E00;&#x4E2A;&#x7EC8;&#x7AEF;&#xFF0C;&#x59D1;&#x4E14;&#x53EB;&#x5B83;&#x8BB0;&#x4E8B;&#x672C;&#x5427;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x9700;&#x8981;&#x7684;&#x529F;&#x80FD;&#x662F;&#xFF1A;&#x63A5;&#x53D7;&#x952E;&#x76D8;&#x8F93;&#x5165;&#xFF08;&#x53EF;&#x4EE5;&#x88AB;&#x79F0;&#x4E3A;&#x201C;&#x6807;&#x51C6;&#x8F93;&#x5165;&#x201D;&#xFF09;&#x7684;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x3002;</p>
<p>&#x4E3A;&#x6B64;&#x6211;&#x4EEC;&#x9700;&#x7EA6;&#x5B9A;&#x8FD9;&#x6837;&#x4E00;&#x4E2A;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#xFF1A;</p>
<ul>
<li>&#x6587;&#x4EF6;&#x8BFB;&#x5165;&#xFF0C;&#x7CFB;&#x7EDF;&#x8C03;&#x7528; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mtext><mi mathvariant="normal">i</mi><mi mathvariant="normal">d</mi></mtext><mo>=</mo><mn>6</mn><mn>3</mn></mrow><annotation encoding="application/x-tex">\text{id}=63</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.69444em;"></span><span class="strut bottom" style="height:0.69444em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord text textstyle uncramped"><span class="mord mathrm">id</span></span><span class="mrel">=</span><span class="mord mathrm">6</span><span class="mord mathrm">3</span></span></span></span></li>
</ul>
<p>&#x6211;&#x4EEC;&#x5148;&#x5728;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x6A21;&#x677F;&#x4E2D;&#x58F0;&#x660E;&#x8BE5;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/src/syscall.rs</span>

<span class="token keyword">enum</span> <span class="token type-definition class-name">SyscallId</span> <span class="token punctuation">{</span>
    <span class="token class-name">Read</span> <span class="token operator">=</span> <span class="token number">63</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">sys_read</span><span class="token punctuation">(</span>fd<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> base<span class="token punctuation">:</span> <span class="token operator">*</span><span class="token keyword">const</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> len<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">i64</span> <span class="token punctuation">{</span>
    <span class="token function">sys_call</span><span class="token punctuation">(</span><span class="token class-name">SyscallId</span><span class="token punctuation">::</span><span class="token class-name">Read</span><span class="token punctuation">,</span> fd<span class="token punctuation">,</span> base <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> len<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x91CC;&#x7684;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x63A5;&#x53E3;&#x8BBE;&#x8BA1;&#x4E0A;&#x662F;&#x4E00;&#x4E2A;&#x8BB0;&#x4E8B;&#x672C;&#x6240;&#x9700;&#x529F;&#x80FD;&#x66F4;&#x5F3A;&#x7684;&#x6587;&#x4EF6;&#x8BFB;&#x5165;&#xFF1A;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x4E2D;&#xFF0C;<code>fd</code> &#x8868;&#x793A;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#xFF0C;<code>base</code> &#x8868;&#x793A;&#x8981;&#x5C06;&#x8BFB;&#x5165;&#x7684;&#x5185;&#x5BB9;&#x4FDD;&#x5B58;&#x5230;&#x7684;&#x865A;&#x62DF;&#x5730;&#x5740;&#xFF0C;<code>len</code> &#x8868;&#x793A;&#x6700;&#x591A;&#x8BFB;&#x5165;&#x591A;&#x5C11;&#x5B57;&#x8282;&#x3002;&#x5176;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x6210;&#x529F;&#x8BFB;&#x5165;&#x7684;&#x5B57;&#x8282;&#x6570;&#x3002;</p>
<p>&#x65B9;&#x4FBF;&#x8D77;&#x89C1;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x662F;&#x5C06;&#x8FD9;&#x4E2A;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x5C01;&#x88C5;&#x4E00;&#x4E0B;&#x6765;&#x5B9E;&#x73B0;&#x6211;&#x4EEC;&#x6240;&#x9700;&#x7684;&#x529F;&#x80FD;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/src/io.rs</span>

<span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>syscall<span class="token punctuation">::</span></span>sys_read<span class="token punctuation">;</span>

<span class="token comment">// &#x6BCF;&#x4E2A;&#x8FDB;&#x7A0B;&#x9ED8;&#x8BA4;&#x6253;&#x5F00;&#x4E09;&#x4E2A;&#x6587;&#x4EF6;</span>
<span class="token comment">// &#x6807;&#x51C6;&#x8F93;&#x5165; stdin fd = 0</span>
<span class="token comment">// &#x6807;&#x51C6;&#x8F93;&#x51FA; stdout fd = 1</span>
<span class="token comment">// &#x6807;&#x51C6;&#x9519;&#x8BEF;&#x8F93;&#x51FA; stderr fd = 2</span>
<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">STDIN</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

<span class="token comment">// &#x8C03;&#x7528; sys_read &#x4ECE;&#x6807;&#x51C6;&#x8F93;&#x5165;&#x8BFB;&#x5165;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">getc</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">u8</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> <span class="token keyword">mut</span> c <span class="token operator">=</span> <span class="token number">0u8</span><span class="token punctuation">;</span>
    <span class="token macro property">assert_eq!</span><span class="token punctuation">(</span><span class="token function">sys_read</span><span class="token punctuation">(</span><span class="token constant">STDIN</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span><span class="token keyword">mut</span> c<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    c
<span class="token punctuation">}</span>
</code></pre>
<p>&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5229;&#x7528; <code>getc</code> &#x7740;&#x624B;&#x5B9E;&#x73B0;&#x6211;&#x4EEC;&#x7684;&#x8BB0;&#x4E8B;&#x672C;&#x4E86;&#xFF01;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/src/bin/notebook.rs</span>
<span class="token attribute attr-name">#![no_std]</span>
<span class="token attribute attr-name">#![no_main]</span>

<span class="token attribute attr-name">#[macro_use]</span>
<span class="token keyword">extern</span> <span class="token keyword">crate</span> <span class="token module-declaration namespace">user</span><span class="token punctuation">;</span>

<span class="token keyword">use</span> <span class="token namespace">user<span class="token punctuation">::</span>io<span class="token punctuation">::</span></span>getc<span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token namespace">user<span class="token punctuation">::</span>io<span class="token punctuation">::</span></span>putchar<span class="token punctuation">;</span>

<span class="token keyword">const</span> <span class="token constant">LF</span><span class="token punctuation">:</span> <span class="token keyword">u8</span> <span class="token operator">=</span> <span class="token number">0x0au8</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token constant">CR</span><span class="token punctuation">:</span> <span class="token keyword">u8</span> <span class="token operator">=</span> <span class="token number">0x0du8</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token constant">BS</span><span class="token punctuation">:</span> <span class="token keyword">u8</span> <span class="token operator">=</span> <span class="token number">0x08u8</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token constant">DL</span><span class="token punctuation">:</span> <span class="token keyword">u8</span> <span class="token operator">=</span> <span class="token number">0x7fu8</span><span class="token punctuation">;</span>

<span class="token attribute attr-name">#[no_mangle]</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;Welcome to notebook!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> <span class="token keyword">mut</span> line_count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">loop</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> c <span class="token operator">=</span> <span class="token function">getc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">match</span> c <span class="token punctuation">{</span>
            <span class="token constant">LF</span> <span class="token operator">|</span> <span class="token constant">CR</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                line_count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
                <span class="token macro property">print!</span><span class="token punctuation">(</span><span class="token string">&quot;{}&quot;</span><span class="token punctuation">,</span> <span class="token constant">LF</span> <span class="token keyword">as</span> <span class="token keyword">char</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token macro property">print!</span><span class="token punctuation">(</span><span class="token string">&quot;{}&quot;</span><span class="token punctuation">,</span> <span class="token constant">CR</span> <span class="token keyword">as</span> <span class="token keyword">char</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token constant">DL</span> <span class="token operator">=&gt;</span> <span class="token keyword">if</span> line_count <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token punctuation">{</span>
                <span class="token comment">// &#x652F;&#x6301;&#x9000;&#x683C;&#x952E;</span>
                <span class="token comment">// &#x5199;&#x6CD5;&#x6765;&#x6E90;&#xFF1A;https://github.com/mit-pdos/xv6-riscv-fall19/blob/xv6-riscv-fall19/kernel/console.c#L41</span>
                <span class="token function">putchar</span><span class="token punctuation">(</span><span class="token constant">BS</span> <span class="token keyword">as</span> <span class="token keyword">char</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token function">putchar</span><span class="token punctuation">(</span><span class="token char string">&apos; &apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token function">putchar</span><span class="token punctuation">(</span><span class="token constant">BS</span> <span class="token keyword">as</span> <span class="token keyword">char</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                line_count <span class="token operator">-=</span> <span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            _ <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                line_count <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span>
                <span class="token macro property">print!</span><span class="token punctuation">(</span><span class="token string">&quot;{}&quot;</span><span class="token punctuation">,</span> c <span class="token keyword">as</span> <span class="token keyword">char</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x5C31;&#x662F;&#x5C06;&#x63A5;&#x53D7;&#x5230;&#x7684;&#x5B57;&#x7B26;&#x6253;&#x5370;&#x5230;&#x5C4F;&#x5E55;&#x4E0A;&#x3002;</p>
<p>&#x770B;&#x4E00;&#x4E0B; <code>getc</code> &#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x6211;&#x4EEC;&#x6EE1;&#x6000;&#x4FE1;&#x5FC3; <code>sys_read</code> &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>1</mn></mrow><annotation encoding="application/x-tex">1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.64444em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">1</span></span></span></span> &#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x786E;&#x4FDD;&#x4E00;&#x5B9A;&#x80FD;&#x591F;&#x8BFB;&#x5230;&#x5B57;&#x7B26;&#x3002;&#x4E0D;&#x8FC7;&#x771F;&#x7684;&#x662F;&#x8FD9;&#x6837;&#x5417;&#xFF1F;</p>
<h3 id="&#x7F13;&#x51B2;&#x533A;">&#x7F13;&#x51B2;&#x533A;</h3>
<p>&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x6211;&#x4EEC;&#x7528;&#x4E00;&#x4E2A;&#x7F13;&#x51B2;&#x533A;&#x6765;&#x8868;&#x793A;&#x6807;&#x51C6;&#x8F93;&#x5165;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x5C06;&#x5176;&#x770B;&#x4F5C;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x961F;&#x5217;&#x3002;</p>
<ul>
<li>&#x952E;&#x76D8;&#x662F;&#x751F;&#x4EA7;&#x8005;&#xFF1A;&#x6BCF;&#x5F53;&#x4F60;&#x6309;&#x4E0B;&#x952E;&#x76D8;&#xFF0C;&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x5B57;&#x7B26;&#x4F1A;&#x52A0;&#x5165;&#x961F;&#x5C3E;&#xFF1B;</li>
<li><code>sys_read</code> &#x662F;&#x6D88;&#x8D39;&#x8005;&#xFF1A;&#x6BCF;&#x5F53;&#x8C03;&#x7528; <code>sys_read</code> &#x51FD;&#x6570;&#xFF0C;&#x4F1A;&#x5C06;&#x961F;&#x5934;&#x7684;&#x5B57;&#x7B26;&#x53D6;&#x51FA;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x3002;</li>
</ul>
<p>&#x5728; <code>sys_read</code> &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5982;&#x679C;&#x961F;&#x5217;&#x4E0D;&#x662F;&#x7A7A;&#x7684;&#xFF0C;&#x90A3;&#x4E48;&#x4E00;&#x5207;&#x90FD;&#x597D;&#xFF1B;&#x5982;&#x679C;&#x961F;&#x5217;&#x662F;&#x7A7A;&#x7684;&#xFF0C;&#x7531;&#x4E8E;&#x5B83;&#x8981;&#x4FDD;&#x8BC1;&#x80FD;&#x591F;&#x8BFB;&#x5230;&#x5B57;&#x7B26;&#xFF0C;&#x56E0;&#x6B64;&#x5B83;&#x53EA;&#x80FD;&#x591F;&#x7B49;&#x5230;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x961F;&#x5217;&#x4E2D;&#x52A0;&#x5165;&#x4E86;&#x65B0;&#x7684;&#x5143;&#x7D20;&#x518D;&#x8FD4;&#x56DE;&#x3002;</p>
<p>&#x800C;&#x8FD9;&#x91CC;&#x7684;&#x201C;&#x7B49;&#x201D;&#xFF0C;&#x53C8;&#x6709;&#x4E24;&#x79CD;&#x7B49;&#x6CD5;&#xFF1A;</p>
<p>&#x6700;&#x7B80;&#x5355;&#x7684;&#x7B49;&#x6CD5;&#x662F;&#xFF1A;&#x5728;&#x539F;&#x5730; <code>while (q.empty()) {}</code> &#x3002;&#x4E5F;&#x5C31;&#x662F;&#x77E5;&#x9053;&#x961F;&#x5217;&#x975E;&#x7A7A;&#x624D;&#x8DF3;&#x51FA;&#x5FAA;&#x73AF;&#xFF0C;&#x53D6;&#x51FA;&#x961F;&#x5934;&#x7684;&#x5B57;&#x7B26;&#x5E76;&#x8FD4;&#x56DE;&#x3002;</p>
<p>&#x53E6;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x662F;&#xFF1A;&#x5F53; <code>sys_read</code> &#x53D1;&#x73B0;&#x961F;&#x5217;&#x662F;&#x7A7A;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x81EA;&#x52A8;&#x653E;&#x5F03; CPU &#x8D44;&#x6E90;&#x8FDB;&#x5165;&#x7761;&#x7720;&#xFF08;&#x6216;&#x79F0;&#x963B;&#x585E;&#xFF09;&#x72B6;&#x6001;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x4ECE;&#x8C03;&#x5EA6;&#x5355;&#x5143;&#x4E2D;&#x79FB;&#x9664;&#x5F53;&#x524D;&#x6240;&#x5728;&#x7EBF;&#x7A0B;&#xFF0C;&#x4E0D;&#x518D;&#x53C2;&#x4E0E;&#x8C03;&#x5EA6;&#x3002;&#x800C;&#x7B49;&#x5230;&#x67D0;&#x65F6;&#x523B;&#x6309;&#x4E0B;&#x952E;&#x76D8;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53D1;&#x73B0;&#x6709;&#x4E2A;&#x7EBF;&#x7A0B;&#x5728;&#x7B49;&#x7740;&#x8FD9;&#x4E2A;&#x961F;&#x5217;&#x975E;&#x7A7A;&#xFF0C;&#x4E8E;&#x662F;&#x8D76;&#x5FEB;&#x5C06;&#x5B83;&#x5524;&#x9192;&#xFF0C;&#x91CD;&#x65B0;&#x52A0;&#x5165;&#x8C03;&#x5EA6;&#x5355;&#x5143;&#xFF0C;&#x7B49;&#x5F85; CPU &#x8D44;&#x6E90;&#x5206;&#x914D;&#x8FC7;&#x6765;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x3002;</p>
<p>&#x540E;&#x8005;&#x76F8;&#x6BD4;&#x524D;&#x8005;&#x7684;&#x597D;&#x5904;&#x5728;&#x4E8E;&#xFF1A;&#x524D;&#x8005;&#x5360;&#x7528;&#x4E86; CPU &#x8D44;&#x6E90;&#x5374;&#x4E0D;&#x5E72;&#x6D3B;&#xFF0C;&#x53EA;&#x662F;&#x5728;&#x539F;&#x5730;&#x7B49;&#x7740;&#xFF1B;&#x800C;&#x540E;&#x8005;&#x867D;&#x7136;&#x4E5F;&#x6CA1;&#x6CD5;&#x5E72;&#x6D3B;&#xFF0C;&#x5374;&#x5F88;&#x6709;&#x81EA;&#x77E5;&#x4E4B;&#x660E;&#x7684;&#x628A; CPU &#x8D44;&#x6E90;&#x8BA9;&#x7ED9;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x4F7F;&#x7528;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x63D0;&#x9AD8;&#x4E86; CPU &#x7684;&#x5229;&#x7528;&#x7387;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5C31;&#x4F7F;&#x7528;&#x540E;&#x8005;&#x6765;&#x5B9E;&#x73B0; <code>sys_read</code> &#x3002;</p>
<h3 id="&#x6761;&#x4EF6;&#x53D8;&#x91CF;">&#x6761;&#x4EF6;&#x53D8;&#x91CF;</h3>
<p>&#x8FD9;&#x79CD;&#x7EBF;&#x7A0B;&#x5C06; CPU &#x8D44;&#x6E90;&#x653E;&#x5F03;&#xFF0C;&#x5E76;&#x7B49;&#x5230;&#x67D0;&#x4E2A;&#x6761;&#x4EF6;&#x6EE1;&#x8DB3;&#x624D;&#x51C6;&#x5907;&#x7EE7;&#x7EED;&#x8FD0;&#x884C;&#x7684;&#x673A;&#x5236;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x6761;&#x4EF6;&#x53D8;&#x91CF; (Condition Variable) &#x6765;&#x63CF;&#x8FF0;&#x3002;&#x800C;&#x5B83;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x9700;&#x8981;&#x4F9D;&#x8D56;&#x51E0;&#x4E2A;&#x65B0;&#x7684;&#x7EBF;&#x7A0B;&#x8C03;&#x5EA6;&#x673A;&#x5236;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/process/mod.rs</span>

<span class="token comment">// &#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x81EA;&#x52A8;&#x653E;&#x5F03; CPU &#x8D44;&#x6E90;&#x5E76;&#x8FDB;&#x5165;&#x963B;&#x585E;&#x72B6;&#x6001;</span>
<span class="token comment">// &#x7EBF;&#x7A0B;&#x72B6;&#x6001;&#xFF1A; Running(Tid) -&gt; Sleeping</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">yield_now</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token constant">CPU</span><span class="token punctuation">.</span><span class="token function">yield_now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// &#x67D0;&#x4E9B;&#x6761;&#x4EF6;&#x6EE1;&#x8DB3;&#xFF0C;&#x7EBF;&#x7A0B;&#x7B49;&#x5F85; CPU &#x8D44;&#x6E90;&#x4ECE;&#x800C;&#x7EE7;&#x7EED;&#x6267;&#x884C;</span>
<span class="token comment">// &#x7EBF;&#x7A0B;&#x72B6;&#x6001;&#xFF1A; Sleeping -&gt; Ready</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">wake_up</span><span class="token punctuation">(</span>tid<span class="token punctuation">:</span> <span class="token class-name">Tid</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token constant">CPU</span><span class="token punctuation">.</span><span class="token function">wake_up</span><span class="token punctuation">(</span>tid<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// &#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x7684; Tid</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">current_tid</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">usize</span> <span class="token punctuation">{</span>
    <span class="token constant">CPU</span><span class="token punctuation">.</span><span class="token function">current_tid</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">// src/process/processor.rs</span>

<span class="token keyword">impl</span> <span class="token class-name">Processor</span> <span class="token punctuation">{</span>
    <span class="token punctuation">...</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">yield_now</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> inner <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">inner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token operator">!</span>inner<span class="token punctuation">.</span>current<span class="token punctuation">.</span><span class="token function">is_none</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">unsafe</span> <span class="token punctuation">{</span>
                <span class="token comment">// &#x7531;&#x4E8E;&#x8981;&#x8FDB;&#x5165; idle &#x7EBF;&#x7A0B;&#xFF0C;&#x5FC5;&#x987B;&#x5173;&#x95ED;&#x5F02;&#x6B65;&#x4E2D;&#x65AD;</span>
                <span class="token comment">// &#x624B;&#x52A8;&#x4FDD;&#x5B58;&#x4E4B;&#x524D;&#x7684; sstatus</span>
                <span class="token keyword">let</span> flags <span class="token operator">=</span> <span class="token function">disable_and_store</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">let</span> tid <span class="token operator">=</span> inner<span class="token punctuation">.</span>current<span class="token punctuation">.</span><span class="token function">as_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token number">0</span><span class="token punctuation">;</span>
                <span class="token keyword">let</span> thread_info <span class="token operator">=</span> inner<span class="token punctuation">.</span>pool<span class="token punctuation">.</span>threads<span class="token punctuation">[</span>tid<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">as_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">expect</span><span class="token punctuation">(</span><span class="token string">&quot;thread not existed when yielding&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment">// &#x4FEE;&#x6539;&#x7EBF;&#x7A0B;&#x72B6;&#x6001;</span>
                thread_info<span class="token punctuation">.</span>status <span class="token operator">=</span> <span class="token class-name">Status</span><span class="token punctuation">::</span><span class="token class-name">Sleeping</span><span class="token punctuation">;</span>
                <span class="token comment">// &#x5207;&#x6362;&#x5230; idle &#x7EBF;&#x7A0B;</span>
                inner<span class="token punctuation">.</span>current
                    <span class="token punctuation">.</span><span class="token function">as_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                    <span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                    <span class="token punctuation">.</span><span class="token number">1</span>
                    <span class="token punctuation">.</span><span class="token function">switch_to</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token operator">*</span>inner<span class="token punctuation">.</span>idle<span class="token punctuation">)</span><span class="token punctuation">;</span>

                <span class="token comment">// &#x4ECE; idle &#x7EBF;&#x7A0B;&#x5207;&#x6362;&#x56DE;&#x6765;</span>
                <span class="token comment">// &#x6062;&#x590D; sstatus</span>
                <span class="token function">restore</span><span class="token punctuation">(</span>flags<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">wake_up</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">,</span> tid<span class="token punctuation">:</span> <span class="token class-name">Tid</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> inner <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">inner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        inner<span class="token punctuation">.</span>pool<span class="token punctuation">.</span><span class="token function">wakeup</span><span class="token punctuation">(</span>tid<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">current_tid</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">usize</span> <span class="token punctuation">{</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">inner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>current<span class="token punctuation">.</span><span class="token function">as_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token number">0</span> <span class="token keyword">as</span> <span class="token keyword">usize</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// src/process/thread_pool.rs</span>

<span class="token comment">// &#x6539;&#x6210; public</span>
<span class="token keyword">pub</span> <span class="token keyword">struct</span> <span class="token type-definition class-name">ThreadInfo</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x6539;&#x6210; public</span>
    <span class="token keyword">pub</span> status<span class="token punctuation">:</span> <span class="token class-name">Status</span><span class="token punctuation">,</span>
    <span class="token keyword">pub</span> thread<span class="token punctuation">:</span> <span class="token class-name">Option</span><span class="token operator">&lt;</span><span class="token class-name">Box</span><span class="token operator">&lt;</span><span class="token class-name">Thread</span><span class="token operator">&gt;&gt;</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>

<span class="token keyword">pub</span> <span class="token keyword">struct</span> <span class="token type-definition class-name">ThreadPool</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x6539;&#x6210; public</span>
    <span class="token keyword">pub</span> threads<span class="token punctuation">:</span> <span class="token class-name">Vec</span><span class="token operator">&lt;</span><span class="token class-name">Option</span><span class="token operator">&lt;</span><span class="token class-name">ThreadInfo</span><span class="token operator">&gt;&gt;</span><span class="token punctuation">,</span>
    scheduler<span class="token punctuation">:</span> <span class="token class-name">Box</span><span class="token operator">&lt;</span><span class="token keyword">dyn</span> <span class="token class-name">Scheduler</span><span class="token operator">&gt;</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>

<span class="token keyword">impl</span> <span class="token class-name">ThreadPool</span> <span class="token punctuation">{</span>
    <span class="token punctuation">...</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">wakeup</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span> tid<span class="token punctuation">:</span> <span class="token class-name">Tid</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> proc <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>threads<span class="token punctuation">[</span>tid<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">as_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">expect</span><span class="token punctuation">(</span><span class="token string">&quot;thread not exist when waking up&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        proc<span class="token punctuation">.</span>status <span class="token operator">=</span> <span class="token class-name">Status</span><span class="token punctuation">::</span><span class="token class-name">Ready</span><span class="token punctuation">;</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>scheduler<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>tid<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x7528;&#x8FD9;&#x51E0;&#x79CD;&#x7EBF;&#x7A0B;&#x8C03;&#x5EA6;&#x673A;&#x5236;&#x6765;&#x5B9E;&#x73B0;&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/lib.rs</span>

<span class="token keyword">mod</span> <span class="token module-declaration namespace">sync</span><span class="token punctuation">;</span>

<span class="token comment">// src/sync/mod.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">mod</span> <span class="token module-declaration namespace">condvar</span><span class="token punctuation">;</span>

<span class="token comment">// src/sync/condvar.rs</span>

<span class="token keyword">use</span> <span class="token namespace">spin<span class="token punctuation">::</span></span><span class="token class-name">Mutex</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token namespace">alloc<span class="token punctuation">::</span>collections<span class="token punctuation">::</span></span><span class="token class-name">VecDeque</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>process<span class="token punctuation">::</span></span><span class="token punctuation">{</span> <span class="token class-name">Tid</span><span class="token punctuation">,</span> current_tid<span class="token punctuation">,</span> yield_now<span class="token punctuation">,</span> wake_up <span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token attribute attr-name">#[derive(Default)]</span>
<span class="token keyword">pub</span> <span class="token keyword">struct</span> <span class="token type-definition class-name">Condvar</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x52A0;&#x4E86;&#x4E92;&#x65A5;&#x9501;&#x7684; Tid &#x961F;&#x5217;</span>
    <span class="token comment">// &#x5B58;&#x653E;&#x7B49;&#x5F85;&#x6B64;&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#x7684;&#x4F17;&#x591A;&#x7EBF;&#x7A0B;</span>
    wait_queue<span class="token punctuation">:</span> <span class="token class-name">Mutex</span><span class="token operator">&lt;</span><span class="token class-name">VecDeque</span><span class="token operator">&lt;</span><span class="token class-name">Tid</span><span class="token operator">&gt;&gt;</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>

<span class="token keyword">impl</span> <span class="token class-name">Condvar</span> <span class="token punctuation">{</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">Self</span> <span class="token punctuation">{</span>
        <span class="token class-name">Condvar</span><span class="token punctuation">::</span><span class="token function">default</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// &#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x7B49;&#x5F85;&#x67D0;&#x79CD;&#x6761;&#x4EF6;&#x6EE1;&#x8DB3;&#x624D;&#x80FD;&#x7EE7;&#x7EED;&#x6267;&#x884C;</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">wait</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x5C06;&#x5F53;&#x524D; Tid &#x52A0;&#x5165;&#x6B64;&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#x7684;&#x7B49;&#x5F85;&#x961F;&#x5217;</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>wait_queue
            <span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">current_tid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// &#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x653E;&#x5F03; CPU &#x8D44;&#x6E90;</span>
        <span class="token function">yield_now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// &#x6761;&#x4EF6;&#x6EE1;&#x8DB3;</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">notify</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x5F39;&#x51FA;&#x7B49;&#x5F85;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;</span>
        <span class="token keyword">let</span> tid <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>wait_queue<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">pop_front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token keyword">let</span> <span class="token class-name">Some</span><span class="token punctuation">(</span>tid<span class="token punctuation">)</span> <span class="token operator">=</span> tid <span class="token punctuation">{</span>
            <span class="token comment">// &#x5524;&#x9192;&#x8BE5;&#x7EBF;&#x7A0B;</span>
            <span class="token function">wake_up</span><span class="token punctuation">(</span>tid<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8BB2;&#x6E05;&#x695A;&#x4E86;&#x673A;&#x5236;&#xFF0C;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x770B;&#x4E00;&#x4E0B;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x3002;</p>
<h3 id="&#x7F13;&#x51B2;&#x533A;&#x5B9E;&#x73B0;">&#x7F13;&#x51B2;&#x533A;&#x5B9E;&#x73B0;</h3>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/fs/mod.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">mod</span> <span class="token module-declaration namespace">stdio</span><span class="token punctuation">;</span>

<span class="token comment">// src/fs/stdio.rs</span>

<span class="token keyword">use</span> <span class="token namespace">alloc<span class="token punctuation">::</span></span><span class="token punctuation">{</span> <span class="token namespace">collections<span class="token punctuation">::</span></span><span class="token class-name">VecDeque</span><span class="token punctuation">,</span> <span class="token namespace">sync<span class="token punctuation">::</span></span><span class="token class-name">Arc</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token namespace">spin<span class="token punctuation">::</span></span><span class="token class-name">Mutex</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>process</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>sync<span class="token punctuation">::</span>condvar<span class="token punctuation">::</span></span><span class="token operator">*</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token namespace">lazy_static<span class="token punctuation">::</span></span><span class="token operator">*</span><span class="token punctuation">;</span>

<span class="token keyword">pub</span> <span class="token keyword">struct</span> <span class="token type-definition class-name">Stdin</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x5B57;&#x7B26;&#x961F;&#x5217;</span>
    buf<span class="token punctuation">:</span> <span class="token class-name">Mutex</span><span class="token operator">&lt;</span><span class="token class-name">VecDeque</span><span class="token operator">&lt;</span><span class="token keyword">char</span><span class="token operator">&gt;&gt;</span><span class="token punctuation">,</span>
    <span class="token comment">// &#x6761;&#x4EF6;&#x53D8;&#x91CF;</span>
    pushed<span class="token punctuation">:</span> <span class="token class-name">Condvar</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>

<span class="token keyword">impl</span> <span class="token class-name">Stdin</span> <span class="token punctuation">{</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">Self</span> <span class="token punctuation">{</span>
        <span class="token class-name">Stdin</span> <span class="token punctuation">{</span>
            buf<span class="token punctuation">:</span> <span class="token class-name">Mutex</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token class-name">VecDeque</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            pushed<span class="token punctuation">:</span> <span class="token class-name">Condvar</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// &#x751F;&#x4EA7;&#x8005;&#xFF1A;&#x8F93;&#x5165;&#x5B57;&#x7B26;</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">push</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">,</span> ch<span class="token punctuation">:</span> <span class="token keyword">char</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x5C06;&#x5B57;&#x7B26;&#x52A0;&#x5165;&#x5B57;&#x7B26;&#x961F;&#x5217;</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>buf
            <span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>ch<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// &#x5982;&#x679C;&#x6B64;&#x65F6;&#x6709;&#x7EBF;&#x7A0B;&#x6B63;&#x5728;&#x7B49;&#x5F85;&#x961F;&#x5217;&#x975E;&#x7A7A;&#x624D;&#x80FD;&#x7EE7;&#x7EED;&#x4E0B;&#x53BB;</span>
        <span class="token comment">// &#x5C06;&#x5176;&#x5524;&#x9192;</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>pushed<span class="token punctuation">.</span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// &#x6D88;&#x8D39;&#x8005;&#xFF1A;&#x53D6;&#x51FA;&#x5B57;&#x7B26;</span>
    <span class="token comment">// &#x8FD0;&#x884C;&#x5728;&#x8BF7;&#x6C42;&#x5B57;&#x7B26;&#x8F93;&#x5165;&#x7684;&#x7EBF;&#x7A0B;&#x4E0A;</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">pop</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">char</span> <span class="token punctuation">{</span>
        <span class="token keyword">loop</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x5C06;&#x4EE3;&#x7801;&#x653E;&#x5728; loop &#x91CC;&#x9762;&#x9632;&#x6B62;&#x518D;&#x590D;&#x5236;&#x4E00;&#x904D;</span>

            <span class="token comment">// &#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x961F;&#x9996;&#x5B57;&#x7B26;</span>
            <span class="token keyword">let</span> ret <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>buf<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">pop_front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">match</span> ret <span class="token punctuation">{</span>
                <span class="token class-name">Some</span><span class="token punctuation">(</span>ch<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                    <span class="token comment">// &#x83B7;&#x53D6;&#x5230;&#x4E86;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;</span>
                    <span class="token keyword">return</span> ch<span class="token punctuation">;</span>
                <span class="token punctuation">}</span><span class="token punctuation">,</span>
                <span class="token class-name">None</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                    <span class="token comment">// &#x5426;&#x5219;&#x961F;&#x5217;&#x4E3A;&#x7A7A;&#xFF0C;&#x901A;&#x8FC7; getc -&gt; sys_read &#x83B7;&#x53D6;&#x5B57;&#x7B26;&#x7684;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x653E;&#x5F03; CPU &#x8D44;&#x6E90;</span>
                    <span class="token comment">// &#x8FDB;&#x5165;&#x963B;&#x585E;&#x72B6;&#x6001;&#x7B49;&#x5F85;&#x5524;&#x9192;</span>
                    <span class="token keyword">self</span><span class="token punctuation">.</span>pushed<span class="token punctuation">.</span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

                    <span class="token comment">// &#x88AB;&#x5524;&#x9192;&#x540E;&#x56DE;&#x5230;&#x5FAA;&#x73AF;&#x5F00;&#x5934;&#xFF0C;&#x6B64;&#x65F6;&#x53EF;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token macro property">lazy_static!</span> <span class="token punctuation">{</span>
    <span class="token keyword">pub</span> <span class="token keyword">static</span> <span class="token keyword">ref</span> <span class="token constant">STDIN</span><span class="token punctuation">:</span> <span class="token class-name">Arc</span><span class="token operator">&lt;</span><span class="token class-name">Stdin</span><span class="token operator">&gt;</span> <span class="token operator">=</span> <span class="token class-name">Arc</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token class-name">Stdin</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="&#x751F;&#x4EA7;&#x8005;&#xFF1A;&#x952E;&#x76D8;&#x4E2D;&#x65AD;">&#x751F;&#x4EA7;&#x8005;&#xFF1A;&#x952E;&#x76D8;&#x4E2D;&#x65AD;</h3>
<p>&#x9996;&#x5148;&#x6211;&#x4EEC;&#x8981;&#x80FD;&#x63A5;&#x53D7;&#x5230;&#x5916;&#x90E8;&#x4E2D;&#x65AD;&#xFF0C;&#x800C; <code>OpenSBI</code> &#x9ED8;&#x8BA4;&#x5C06;&#x5916;&#x90E8;&#x4E2D;&#x65AD;&#x548C;&#x4E32;&#x53E3;&#x5F00;&#x5173;&#x90FD;&#x5173;&#x4E0A;&#x4E86;&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x624B;&#x52A8;&#x5C06;&#x4ED6;&#x4EEC;&#x6253;&#x5F00;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/interrupt.rs</span>

<span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>memory<span class="token punctuation">::</span></span>access_pa_via_va<span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token namespace">riscv<span class="token punctuation">::</span>register<span class="token punctuation">::</span></span>sie<span class="token punctuation">;</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">...</span>

    <span class="token comment">// enable external interrupt</span>
    <span class="token namespace">sie<span class="token punctuation">::</span></span><span class="token function">set_sext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// closed by OpenSBI, so we open them manually</span>
    <span class="token comment">// see https://github.com/rcore-os/rCore/blob/54fddfbe1d402ac1fafd9d58a0bd4f6a8dd99ece/kernel/src/arch/riscv32/board/virt/mod.rs#L4</span>
    <span class="token function">init_external_interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">enable_serial_interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">pub</span> <span class="token keyword">unsafe</span> <span class="token keyword">fn</span> <span class="token function-definition function">init_external_interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> <span class="token constant">HART0_S_MODE_INTERRUPT_ENABLES</span><span class="token punctuation">:</span> <span class="token operator">*</span><span class="token keyword">mut</span> <span class="token keyword">u32</span> <span class="token operator">=</span> <span class="token function">access_pa_via_va</span><span class="token punctuation">(</span><span class="token number">0x0c00_2080</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token operator">*</span><span class="token keyword">mut</span> <span class="token keyword">u32</span><span class="token punctuation">;</span>
    <span class="token keyword">const</span> <span class="token constant">SERIAL</span><span class="token punctuation">:</span> <span class="token keyword">u32</span> <span class="token operator">=</span> <span class="token number">0xa</span><span class="token punctuation">;</span>
    <span class="token constant">HART0_S_MODE_INTERRUPT_ENABLES</span><span class="token punctuation">.</span><span class="token function">write_volatile</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token constant">SERIAL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">pub</span> <span class="token keyword">unsafe</span> <span class="token keyword">fn</span> <span class="token function-definition function">enable_serial_interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> <span class="token constant">UART16550</span><span class="token punctuation">:</span> <span class="token operator">*</span><span class="token keyword">mut</span> <span class="token keyword">u8</span> <span class="token operator">=</span> <span class="token function">access_pa_via_va</span><span class="token punctuation">(</span><span class="token number">0x10000000</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token operator">*</span><span class="token keyword">mut</span> <span class="token keyword">u8</span><span class="token punctuation">;</span>
    <span class="token constant">UART16550</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">write_volatile</span><span class="token punctuation">(</span><span class="token number">0x0B</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token constant">UART16550</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">write_volatile</span><span class="token punctuation">(</span><span class="token number">0x01</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x91CC;&#x7684;&#x5185;&#x5B58;&#x5C1A;&#x672A;&#x88AB;&#x6620;&#x5C04;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x5185;&#x5B58;&#x6A21;&#x5757;&#x521D;&#x59CB;&#x5316;&#x65F6;&#x5B8C;&#x6210;&#x6620;&#x5C04;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/memory/mod.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">kernel_remap</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> <span class="token keyword">mut</span> memory_set <span class="token operator">=</span> <span class="token class-name">MemorySet</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">extern</span> <span class="token string">&quot;C&quot;</span> <span class="token punctuation">{</span>
        <span class="token keyword">fn</span> <span class="token function-definition function">bootstack</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">fn</span> <span class="token function-definition function">bootstacktop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    memory_set<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>
        bootstack <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
        bootstacktop <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
        <span class="token class-name">MemoryAttr</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token class-name">Linear</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token constant">PHYSICAL_MEMORY_OFFSET</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token class-name">None</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
    memory_set<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>
        <span class="token function">access_pa_via_va</span><span class="token punctuation">(</span><span class="token number">0x0c00_2000</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token function">access_pa_via_va</span><span class="token punctuation">(</span><span class="token number">0x0c00_3000</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token class-name">MemoryAttr</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token class-name">Linear</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token constant">PHYSICAL_MEMORY_OFFSET</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token class-name">None</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
    memory_set<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>
        <span class="token function">access_pa_via_va</span><span class="token punctuation">(</span><span class="token number">0x1000_0000</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token function">access_pa_via_va</span><span class="token punctuation">(</span><span class="token number">0x1000_1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token class-name">MemoryAttr</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token class-name">Linear</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token constant">PHYSICAL_MEMORY_OFFSET</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token class-name">None</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">unsafe</span> <span class="token punctuation">{</span>
        memory_set<span class="token punctuation">.</span><span class="token function">activate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E5F;&#x56E0;&#x6B64;&#xFF0C;&#x5185;&#x5B58;&#x6A21;&#x5757;&#x8981;&#x6BD4;&#x4E2D;&#x65AD;&#x6A21;&#x5757;&#x5148;&#x521D;&#x59CB;&#x5316;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/init.rs</span>

<span class="token attribute attr-name">#[no_mangle]</span>
<span class="token keyword">pub</span> <span class="token keyword">extern</span> <span class="token string">&quot;C&quot;</span> <span class="token keyword">fn</span> <span class="token function-definition function">rust_main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token operator">!</span> <span class="token punctuation">{</span>
    <span class="token keyword">extern</span> <span class="token string">&quot;C&quot;</span> <span class="token punctuation">{</span>
        <span class="token keyword">fn</span> <span class="token function-definition function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>memory<span class="token punctuation">::</span></span><span class="token function">init</span><span class="token punctuation">(</span>
        <span class="token punctuation">(</span><span class="token punctuation">(</span>end <span class="token keyword">as</span> <span class="token keyword">usize</span> <span class="token operator">-</span> <span class="token constant">KERNEL_BEGIN_VADDR</span> <span class="token operator">+</span> <span class="token constant">KERNEL_BEGIN_PADDR</span><span class="token punctuation">)</span> <span class="token operator">&gt;&gt;</span> <span class="token number">12</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token constant">PHYSICAL_MEMORY_END</span> <span class="token operator">&gt;&gt;</span> <span class="token number">12</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>interrupt<span class="token punctuation">::</span></span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>fs<span class="token punctuation">::</span></span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>process<span class="token punctuation">::</span></span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>timer<span class="token punctuation">::</span></span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>process<span class="token punctuation">::</span></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">loop</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x968F;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x5BF9;&#x5916;&#x90E8;&#x4E2D;&#x65AD;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/interrupt.rs</span>

<span class="token attribute attr-name">#[no_mangle]</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">rust_trap</span><span class="token punctuation">(</span>tf<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token class-name">TrapFrame</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">...</span>
    <span class="token class-name">Trap</span><span class="token punctuation">::</span><span class="token class-name">Interrupt</span><span class="token punctuation">(</span><span class="token class-name">Interrupt</span><span class="token punctuation">::</span><span class="token class-name">SupervisorExternal</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token function">external</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">...</span>
<span class="token punctuation">}</span>

<span class="token keyword">fn</span> <span class="token function-definition function">external</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x952E;&#x76D8;&#x5C5E;&#x4E8E;&#x4E00;&#x79CD;&#x4E32;&#x53E3;&#x8BBE;&#x5907;&#xFF0C;&#x800C;&#x5B9E;&#x9645;&#x4E0A;&#x6709;&#x5F88;&#x591A;&#x79CD;&#x5916;&#x8BBE;</span>
    <span class="token comment">// &#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x53EA;&#x8003;&#x8651;&#x4E32;&#x53E3;</span>
    <span class="token keyword">let</span> _ <span class="token operator">=</span> <span class="token function">try_serial</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">fn</span> <span class="token function-definition function">try_serial</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">bool</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x901A;&#x8FC7; OpenSBI &#x83B7;&#x53D6;&#x4E32;&#x53E3;&#x8F93;&#x5165;</span>
    <span class="token keyword">match</span> <span class="token keyword">super</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>io<span class="token punctuation">::</span></span><span class="token function">getchar_option</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Some</span><span class="token punctuation">(</span>ch<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x5C06;&#x83B7;&#x53D6;&#x5230;&#x7684;&#x5B57;&#x7B26;&#x8F93;&#x5165;&#x6807;&#x51C6;&#x8F93;&#x5165;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>ch <span class="token operator">==</span> <span class="token char string">&apos;\r&apos;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>fs<span class="token punctuation">::</span>stdio<span class="token punctuation">::</span></span><span class="token constant">STDIN</span><span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token char string">&apos;\n&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">else</span> <span class="token punctuation">{</span>
                <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>fs<span class="token punctuation">::</span>stdio<span class="token punctuation">::</span></span><span class="token constant">STDIN</span><span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>ch<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token boolean">true</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token class-name">None</span> <span class="token operator">=&gt;</span> <span class="token boolean">false</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// src/io.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">char</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> c <span class="token operator">=</span> <span class="token namespace">sbi<span class="token punctuation">::</span></span><span class="token function">console_getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">u8</span><span class="token punctuation">;</span>

    <span class="token keyword">match</span> c <span class="token punctuation">{</span>
        <span class="token number">255</span> <span class="token operator">=&gt;</span> <span class="token char string">&apos;\0&apos;</span><span class="token punctuation">,</span>
        c <span class="token operator">=&gt;</span> c <span class="token keyword">as</span> <span class="token keyword">char</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">// &#x8C03;&#x7528; OpenSBI &#x63A5;&#x53E3;</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">getchar_option</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token class-name">Option</span><span class="token operator">&lt;</span><span class="token keyword">char</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> c <span class="token operator">=</span> <span class="token namespace">sbi<span class="token punctuation">::</span></span><span class="token function">console_getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">isize</span><span class="token punctuation">;</span>
    <span class="token keyword">match</span> c <span class="token punctuation">{</span>
        <span class="token operator">-</span><span class="token number">1</span> <span class="token operator">=&gt;</span> <span class="token class-name">None</span><span class="token punctuation">,</span>
        c <span class="token operator">=&gt;</span> <span class="token class-name">Some</span><span class="token punctuation">(</span>c <span class="token keyword">as</span> <span class="token keyword">u8</span> <span class="token keyword">as</span> <span class="token keyword">char</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="&#x6D88;&#x8D39;&#x8005;&#xFF1A;sysread-&#x5B9E;&#x73B0;">&#x6D88;&#x8D39;&#x8005;&#xFF1A;sys_read &#x5B9E;&#x73B0;</h3>
<p>&#x8FD9;&#x5C31;&#x5F88;&#x7B80;&#x5355;&#x4E86;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/syscall.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">SYS_READ</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token number">63</span><span class="token punctuation">;</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">syscall</span><span class="token punctuation">(</span>id<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> args<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token keyword">usize</span><span class="token punctuation">;</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> tf<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token class-name">TrapFrame</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">isize</span> <span class="token punctuation">{</span>
    <span class="token keyword">match</span> id <span class="token punctuation">{</span>
        <span class="token constant">SYS_READ</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token function">sys_read</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token keyword">as</span> <span class="token operator">*</span><span class="token keyword">mut</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> args<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        <span class="token punctuation">...</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x8FD9;&#x91CC; fd, len &#x90FD;&#x6CA1;&#x6709;&#x7528;&#x5230;</span>
<span class="token keyword">fn</span> <span class="token function-definition function">sys_read</span><span class="token punctuation">(</span>fd<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> base<span class="token punctuation">:</span> <span class="token operator">*</span><span class="token keyword">mut</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> len<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">isize</span> <span class="token punctuation">{</span>
    <span class="token keyword">unsafe</span> <span class="token punctuation">{</span>
        <span class="token operator">*</span>base <span class="token operator">=</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>fs<span class="token punctuation">::</span>stdio<span class="token punctuation">::</span></span><span class="token constant">STDIN</span><span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">u8</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x8981;&#x5199;&#x5165;&#x7528;&#x6237;&#x6001;&#x5185;&#x5B58;&#xFF0C;&#x4F46;&#x662F; CPU &#x9ED8;&#x8BA4;&#x5E76;&#x4E0D;&#x5141;&#x8BB8;&#x5728;&#x5185;&#x6838;&#x6001;&#x8BBF;&#x95EE;&#x7528;&#x6237;&#x6001;&#x5185;&#x5B58;&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x8981;&#x5728;&#x5185;&#x5B58;&#x521D;&#x59CB;&#x5316;&#x7684;&#x65F6;&#x5019;&#x5C06;&#x5F00;&#x5173;&#x6253;&#x5F00;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/memory/mod.rs</span>

<span class="token keyword">use</span> <span class="token namespace">riscv<span class="token punctuation">::</span>register<span class="token punctuation">::</span></span>sstatus<span class="token punctuation">;</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">init</span><span class="token punctuation">(</span>l<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> r<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">unsafe</span> <span class="token punctuation">{</span>
        <span class="token namespace">sstatus<span class="token punctuation">::</span></span><span class="token function">set_sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x4EE5;&#x4E0B;&#x4E0D;&#x53D8;</span>
    <span class="token constant">FRAME_ALLOCATOR</span><span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span>l<span class="token punctuation">,</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">init_heap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">kernel_remap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;++++ setup memory!    ++++&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5C06;&#x8981;&#x8FD0;&#x884C;&#x7684;&#x7A0B;&#x5E8F;&#x4ECE; <code>rust/hello_world</code> &#x6539;&#x6210; <code>rust/notebook</code> &#x4E86;&#xFF01;</p>
<p>&#x5C06;&#x591A;&#x4F59;&#x7684;&#x7EBF;&#x7A0B;&#x6362;&#x5165;&#x6362;&#x51FA;&#x63D0;&#x793A;&#x4FE1;&#x606F;&#x5220;&#x6389;&#xFF0C;&#x8FD0;&#x884C;&#x4E00;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x5B9E;&#x73B0;&#x4E86;&#x5B57;&#x7B26;&#x7684;&#x8F93;&#x5165;&#x53CA;&#x663E;&#x793A;&#x4E86;&#xFF01;&#x53EF;&#x4EE5;&#x4EAB;&#x53D7;&#x8F93;&#x5165;&#x5E26;&#x6765;&#x7684;&#x4E50;&#x8DA3;&#x4E86;&#xFF01;&#xFF08;&#x5927;&#x96FE;</p>
<p>&#x5982;&#x679C;&#x8BB0;&#x4E8B;&#x672C;&#x4E0D;&#x80FD;&#x6B63;&#x5E38;&#x5DE5;&#x4F5C;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;<a href="https://github.com/rcore-os/rCore_tutorial/tree/ch9-pa2" target="_blank">&#x8FD9;&#x91CC;</a>&#x627E;&#x5230;&#x5DF2;&#x6709;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<p><div id="page-footer" class="localized-footer"><hr><p><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css"></p>
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<div id="gitalk-container"></div>
<script>
  var gitalk = new Gitalk({
    clientID: "ab871063a9fa7f1da571",
    clientSecret: "4760b384e3e452d2241d859cfa6a7da21a8729ec",
    repo: "rCore_tutorial_doc",
    owner: "rcore-os",
    admin: [
      "chyyuu",
      "xyongcn",
      "wangrunji0408",
      "xy-plus",
      "PanQL",
      "wyfcyx",
      "equation314"
    ],
    id: location.pathname,
    distractionFreeMode: false
  });
  gitalk.render("gitalk-container");
</script>
</div></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="part1.html" class="navigation navigation-prev " aria-label="Previous page: [使用文件系统]">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="part3.html" class="navigation navigation-next " aria-label="Next page: [实现终端]">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"[实现记事本]","level":"1.11.2","depth":2,"next":{"title":"[实现终端]","level":"1.11.3","depth":2,"path":"chapter9/part3.md","ref":"chapter9/part3.md","articles":[]},"previous":{"title":"[使用文件系统]","level":"1.11.1","depth":2,"path":"chapter9/part1.md","ref":"chapter9/part1.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","-highlight","prism","localized-footer","mermaid-gb3"],"pluginsConfig":{"chapter-fold":{},"prism":{"css":["prismjs/themes/prism-tomorrow.css"]},"emphasize":{},"search":{},"localized-footer":{"filename":"extensions/comment/gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"hide-element":{"elements":[".gitbook-link"]},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"mermaid-gb3":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter9/part2.md","mtime":"2020-09-09T11:31:56.907Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-10T06:57:01.716Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

